<template>
  <div class="column-detail-page w-75 mx-auto">
    <div class="column-info row mb-4 border-bottom pb-4 align-items-center" v-if="column">
      <!-- 头像 -->
      <div class="col-3 text-center">
        <img :src="column.avatar.fitUrl" :alt="column.title" class="rounded-circle border w-100">
      </div>
      <!-- 简介 -->
      <div class="col-9">
        <h4>{{column.title}}</h4>
        <p class="text-muted">{{column.description}}</p>
      </div>
    </div>
    <!-- postList -->
    <post-list :list="postList"></post-list>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { GlobalDataProps } from '../store/type'
import PostList from '../components/PostList.vue'
import { generateFitUrl } from '../helper'

export default defineComponent({
  name: 'ColumnDetail',
  components: {
    PostList
  },
  setup () {
    const route = useRoute()
    const store = useStore<GlobalDataProps>()

    const currentId = Number(route.params.id)

    onMounted(() => {
      store.dispatch('fetchColumns', currentId)
      store.dispatch('fetchPosts', currentId)
    })

    const postList = computed(() => {
      const list = store.getters.getPostsByCid(currentId)
      list.map((item: { image: { url: unknown } }) => {
        if (item.image && item.image.url) {
          item.image.url = item.image.url + '?x-oss-process=image/resize,m_pad,h_100,w_100'
        } else {
          item.image = {
            url: require('@/assets/column.jpg')
          }
        }
        return item
      })
      return list
    })

    const column = computed(() => store.getters.getColumnById(currentId))
    // const postList = computed(() => store.getters.getPostsByCid(currentId))

    return {
      column,
      postList
    }
  }
})
</script>

<style scoped>
</style>
